package com.fare.taluo.application.impl.plane.city;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.base.ddd.application.BaseApplicationImpl;
import com.fare.taluo.core.domain.config.city.CityEntity;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.vo.core.CityEntityVO;
import com.fare.taluo.vo.core.PortEntityVO;

@Transactional
@Scope("singleton")
@Service("portApplication")
public class PortApplicationImpl extends BaseApplicationImpl implements PortApplication {
	/**
	 * 将机场entity对象转为VO对象
	 * @param entity
	 * @return
	 */
	private PortEntityVO convert2PortVO(PortEntity entity){
		if(entity == null) return null;
		PortEntityVO vo = new PortEntityVO();
		CityEntity city = entity.getCity();
		vo.setCityNameCode(city.getCode());
		vo.setCityName(entity.getCityName());
		vo.setId(entity.getCode());
		vo.setPortCode(entity.getCode());
		vo.setHasHot(entity.getHotlevel()>0);
		vo.setHasInternation(city.getIsInternational()==1);
		return vo;
	}
	/**
	 * 将城市entity对象转为VO对象
	 * @param entity
	 * @return
	 */
	private CityEntityVO convert2CityVO(CityEntity entity){
		if(entity == null) return null;
		CityEntityVO vo = new CityEntityVO();
		vo.setCode(vo.getCode());
		vo.setName(vo.getName());
		vo.setCreateDate(entity.getCreateDate());
		vo.setIsInternational(entity.getIsInternational());
		vo.setNameSpell(entity.getNameSpell());
		vo.setNameSpellInitials(entity.getNameSpellInitials());
		vo.setPid(entity.getPid());
		vo.setState(entity.getState());
		vo.setTreeLevel(entity.getTreeLevel());
		vo.setTreeSort(entity.getTreeSort());
		vo.setTreeType(entity.getTreeType());
		vo.setUpdateDate(entity.getUpdateDate());
		return vo;
	}
	@Override
	public List<PortEntityVO> findAllPortEntityVO() {
		PortEntity port = new PortEntity();
		List<PortEntity> portList = port.findAll();
		
		List<PortEntityVO> list = new ArrayList<PortEntityVO>();
		for (PortEntity e : portList) {
			list.add(convert2PortVO(e));
		}
		return list;
	}

	@Override
	public List<PortEntityVO> findPortEntityByCityCode(String cityCode) {
		PortEntity portEntitie=new PortEntity();
		List<PortEntity> portList=portEntitie.findPortEntityByCityCode(cityCode);
		List<PortEntityVO> port = new ArrayList<PortEntityVO>();
		if(portList.size()>0&&portList!=null){
			for(PortEntity p:portList){
				port.add(convert2PortVO(p));
			}
		}
		return port;
	}
	
	public List<CityEntityVO> findCityEntityByCityCode(String cityCode) {
		CityEntity cityEntity=new CityEntity();
		List<CityEntity> cityEntities=cityEntity.findCityEntityByCityCode(cityCode);
		List<CityEntityVO> cityEntityVOs=new ArrayList<CityEntityVO>();
		if(cityEntities.size()>0&&cityEntities!=null){
			for(CityEntity e:cityEntities){
				cityEntityVOs.add(convert2CityVO(e));
			}
		}
		return cityEntityVOs;
	}
	
	public PortEntityVO findPortEntityByCityName(String cityName) {
		PortEntity port = new PortEntity();
		List<PortEntity> list = port.findPortEntityByCityName(cityName);
		if(list != null && !list.isEmpty()) port = list.get(0);
		return convert2PortVO(port);
	}

   
	@Override
	public PortEntityVO findCityNameByPortCode(String portCode) {
		PortEntity port = new PortEntity();
		port = port.findByPortCode(portCode);
		return convert2PortVO(port);
	}
   
	public PortEntityVO findPortByPortName(String portName){
		PortEntity port = new PortEntity();
		List<PortEntity> results = port.findPortEntityByPortName(portName);
		if(results != null && !results.isEmpty())port = results.get(0);
		return convert2PortVO(port);
	}
	
	@Override
	public Map<String, PortEntityVO> findPortEntityToMap() {
		PortEntity port = new PortEntity();
		Map<String, PortEntityVO> voMap = new HashMap<String, PortEntityVO>();
		Map<String, PortEntity> entityMap  = port.findPortEntityToMap();
		for (Iterator iterator = entityMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry entry = (Map.Entry) iterator.next();
			String key = (String) entry.getKey();
			PortEntity entity = (PortEntity)entry.getValue();
			PortEntityVO vo = new PortEntityVO();
			vo.setCityName(entity.getCityName());
			vo.setPortCode(entity.getCode());
			vo.setPortName(entity.getName());
			voMap.put(entity.getCode(), vo);
		}	
		return voMap;
	}

	public Map<String,PortEntityVO> findPortEntityToMapByName(){
		PortEntity port = new PortEntity();
		Map<String, PortEntityVO> voMap = new HashMap<String, PortEntityVO>();
		Map<String, PortEntity> entityMap  = port.findPortEntityToMapByName();
		for (Iterator iterator = entityMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry entry = (Map.Entry) iterator.next();
			String key = (String) entry.getKey();
			PortEntity entity = (PortEntity)entry.getValue();
			PortEntityVO vo = new PortEntityVO();
			vo.setCityName(entity.getCityName());
			vo.setPortCode(entity.getCode());
			voMap.put(entity.getCityName(), vo);
		}	
		return voMap;
	}


	@Override
	public List<PortEntityVO> getPortsWithTaskTemplate(String channelKey) {
		List<PortEntityVO> results = new ArrayList<PortEntityVO>();
		String hql = null;
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(channelKey)){
			 hql = "from PortEntity p, CityEntity c where p.cityNameCode = c.code and " +
				"(p.portCode in(select f.fromCity from TaskTemplateEntity f" +
				" where f.attachHbaseKey = ?) or p.portCode in(select f.toCity from TaskTemplateEntity f" +
				" where f.attachHbaseKey = ?))";
			params.add(channelKey);
			params.add(channelKey);
		}else{
			 hql = "from PortEntity p, CityEntity c where p.cityNameCode = c.code and " +
					" (p.portCode in(select f.fromCity from TaskTemplateEntity f)" +
					" or p.portCode in(select f.toCity from TaskTemplateEntity f))";
		}

		List<Object[]> list = super.queryChannel().queryResult(hql,params.toArray());
		PortEntityVO vo = null;
		PortEntity p = null;
		CityEntity c = null;
		for(Object[] o : list){
			p = (PortEntity)o[0];
			c = (CityEntity)o[1];
			vo  = new PortEntityVO();
			vo.setPortCode(p.getCode());
			vo.setPortName(p.getName());
			vo.setCityNameCode(c.getCode());
			vo.setCityName(p.getCityName());
			vo.setNameSpell(c.getNameSpell());
			vo.setNameSpellInitials(c.getNameSpellInitials());
			vo.setHasHot(c.getHotLevel()>0?true:false);
			vo.setHasInternation(c.getIsInternational() != null && c.getIsInternational() == 1);
			
			results.add(vo);
		}
		return results;
	}
	
	@Override
	public List<PortEntityVO> findByCityId(Long pid) {
		List<PortEntity> results = super.queryChannel().queryResult("from PortEntity where city.id=? order by priority",new Object[]{pid});
		List<PortEntityVO> voList = new ArrayList<PortEntityVO>();
		for (PortEntity e : results) {
			voList.add(this.convert2PortVO(e));
		}
		return voList;
	}
}
